
const g = require('../utils/global');

cc.Class({
    extends: cc.Component,

    properties: {
        defaultFrame: {
            type: cc.SpriteFrame,
            default: null,
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.isSelect = false;
    },

    start () {

    },

    // 初始化，设置位置
    initWithModel(model) {
        this.model = model;
        const x = model.startX;
        const y = model.startY;
        // 锚点在中间
        const pos = this.calculatePixel(x, y);
        this.node.x = pos.posX;
        this.node.y = pos.posY;
        // 动画控制器
        const anim = this.node.getComponent(cc.Animation);
        if(model.status === g.CELL_STATUS.common) {
            anim.stop();
        } else {
            anim.play(model.status);
        }
    },

    // 更新动作
    updateView() {
        const cmd = this.model.cmd;
        if (cmd.length < 1) {
            return ;
        }
        const actions = [];
        let curTime = 0;
        for(const item of cmd) {
            if(item.playTime > curTime) {
                const delay = cc.delayTime(item.playTime - curTime);
                actions.push(delay);
            }
            if (item.action === g.CELL_ACTION.moveTo) {
                // 移动
                const pos = this.calculatePixel(item.pos.x, item.pos.y);
                const move = cc.moveTo(g.ANI_TIME.move, cc.v2(pos.posX, pos.posY));
                actions.push(move);
            } else if (item.action === g.CELL_ACTION.toDie) {
                // 消除
                if(this.status === g.CELL_STATUS.bird) {
                    const anim = this.node.getComponent(cc.Animation);
                    anim.play(g.CELL_ANIM.effect);
                    actions.push(cc.delayTime(g.ANI_TIME.bomb_bird_delay));
                }
                const callback = cc.callFunc(function () {
                    this.node.destroy();
                }, this);
                actions.push(callback);
            } else if (item.action === g.CELL_ACTION.setVisible) {
                let isVisible = item.isVisible;
                actions.push(cc.callFunc(function () {
                    if(isVisible) {
                        this.node.opacity = 255;
                    } else {
                        this.node.opacity = 0;
                    }
                }, this));
            } else if (item.action === g.CELL_ACTION.toShake) {
                const rotateRight = cc.rotateBy(0.06, 30);
                const rotateLeft = cc.rotateBy(0.12, -60);
                actions.push(cc.repeat(cc.sequence(rotateRight, rotateLeft, rotateRight), 2));
            }
            // 下一轮播放动画时间
            curTime = item.playTime+item.keepTime;
        }

        // 执行动作
        if(actions.length === 1) {
            this.node.runAction(actions[0])
        } else {
            this.node.runAction(cc.sequence(...actions));
        }
    },

    setSelect(isSelected) {
        const anim = this.node.getComponent(cc.Animation);
        const bg = this.node.getChildByName('select');
        if(isSelected === false && this.isSelect && this.model.status === g.CELL_STATUS.common) {
            // 暂停动画
            anim.stop();
            this.node.getComponent(cc.Sprite).spriteFrame = this.defaultFrame;
        } else if (isSelected && (this.model.status === g.CELL_STATUS.common || this.model.status === g.CELL_STATUS.bird)) {
            // 当前不是被选择对象
            anim.play(g.CELL_ANIM.click);
        }
        bg.active = isSelected;
        this.isSelect = isSelected;
    },

    // 根据格子x，y计算像素
    calculatePixel(x, y) {
        const posX = (g.CELL_WIDTH+ g.OFFSET_X)*(y - 0.5);
        const posY = g.GRID_HEIGHT - (g.OFFSET_Y+ g.CELL_HEIGHT)*(x-0.5);
        return {posX, posY};
    }
    // update (dt) {},
});
